<?php include('navigation.php');
$weixinmenu_m=new M('weixinmenu');
$data=$weixinmenu_m->select('1');
$menudata=array();
foreach($data as $k=>$v){
    if($v['pid']==0){
        $menudata[]=$v;
    }
    if($v['pid']>0){
        for($i=0,$l=count($menudata);$i<$l;$i++){
            if($menudata[$i]['id']==$v['pid']){
                $menudata[$i]['sub_button'][]=$v;
            }
        }
    }
}
?>
<!-- End Navigation -->

<div class="container main-content">
    <!-- DataTables Example -->
    <div class="col-lg-12">
        <div class="row">

            <div class="widget-container fluid-height clearfix">
                <div class="alert alert-warning">
                    只有认证的服务号，和认证的订阅号才能使用次功能
                </div>
                <div class="heading">
                    <i class="icon-comments-alt"></i>微信菜单设置
                </div>
                <div class="widget-content padded form-horizontal">
                	<input class="btn btn-primary" type="button" onclick="openmodal(0)" value="添加主菜单">
                    <input class="btn btn-primary" type="button" onclick="updateweixinmenu()" value="更新微信菜单">
                    <ul>
                        <?php foreach($menudata as $k=>$v){?>
                        <li id="item_<?php echo $v['id'];?>"><?php echo $v['name'];?><a href="javascript:delitem(<?php echo $v['id'];?>)">删除</a>&nbsp;<?php if($v['type']=="sub_button"){ echo '<a href="javascript:openmodal('.$v['id'].')">添加子菜单</a>';} ?>
                        <?php if(isset($v['sub_button'])){?>
                        <ul>
                        <?php foreach($v['sub_button'] as $subitem){?>
                            <li id="item_<?php echo $subitem['id'];?>"><?php echo $subitem['name'];?> <a href="javascript:delitem(<?php echo $subitem['id'];?>)">删除</a></li>
                        <?php }?>
                        </ul>
                        <?php }?>
                        </li>
                        <?php }?>
                    </ul>
                </div>
            </div>
        </div>
        <!--end weixin model-->
    </div>
    <!--weixin_config-->
<div class="modal fade" id="weixinmenu">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
                <h4 class="modal-title">
                    添加菜单
                </h4>
            </div>
            <div class="modal-body">
                    <div class="form-group" >
                        <label class="control-label col-md-3" style="text-align:right;">名称:</label>
                        <div class="col-md-8">
                            <input class="form-control" name="name" value=""
                                   placeholder="顶级菜单最多4个汉字，二级菜单最多7个汉字" type="text">
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="control-label col-md-3" style="text-align:right;">类型：</label>
                        <div class="col-md-8">
                        	<select name="type" class="form-control" >
                        		<option value="click" >click</option>
                        		<option value="view" >view</option>
                        		<option value="sub_button" >sub_button</option>
                        	</select>
                        </div>
                    </div>
                    <div class="form-group" >
                        <label class="control-label col-md-3" style="text-align:right;">值：</label>
                        <div class="col-md-8">
                            <input class="form-control" name="val" value=""
                                   placeholder="请输入触发key值" type="text">
                        </div>
                    </div>
            </div>
            <div class="modal-footer">
            	<input class="form-control" name="pid" value="0" type="hidden">
                <input class="btn btn-primary" type="button" value="确认" onclick="updatemenu();">
                <button class="btn btn-default-outline" data-dismiss="modal" type="button">Close</button>
            </div>
        </div>
    </div>
</div>
<!--end weixin_config-->

</div>
<script type="text/javascript">
    $(function () {
        activatelogin();
        $('select[name=type]').bind('change',function(){
        	var type=$(this).val();
	    	if(type=='sub_button'){
	    		$('input[name=val]').attr('readonly','true');
	    		$('input[name=val]').attr('placeholder','无需填写');
	    	}else if(type=='click'){
				$('input[name=val]').removeAttr('readonly');
	    		$('input[name=val]').attr('placeholder','请输入触发key值');
	    	}else{
	    		$('input[name=val]').removeAttr('readonly');
	    		$('input[name=val]').attr('placeholder','请输入跳转的url');
	    	}
        });
    });
    function openmodal(pid){
        $('input[name=pid]').val(pid);
        $('input[name=name]').val('');
        $('select[name=type]').val('click');
        $('input[name=val]').val('');
    	$('#weixinmenu').modal('show');
    }
    function updatemenu(){
    	var name=$('input[name=name]').val();
    	var type=$('select[name=type]').val();
    	var val=$('input[name=val]').val();
    	var pid=$('input[name=pid]').val();
    	if(name==''){
    		alert('菜单名称不能为空');
    		return false;
    	}
    	if(type!=='sub_button'){
    		if(val==''){
    			alert('菜单属性值不能为空');
    			return false;
    		}
    	}
        if(pid>0 && type=='sub_button'){
            alert('子菜单不能设置二级子菜单');
            return false;
        }
    	$.ajax({
    		'url':'/myadmin/weixinmenu.do.php?do=savemenu',
    		'type':'post',
    		'data':{"name":name,"type":type,"val":val,"pid":pid},
    		'dataType':'json',
    		'success':function(json){
                alert(json.error_msg)
                if(json.error>0){
                    window.location.reload();
                }
    		}
    	})
    }
    
    function delitem(id){
        if(!confirm('确认要删除吗？'))return false;
        $.ajax({
            "url":"/myadmin/weixinmenu.do.php?do=delmenu",
            "type":"post",
            "data":{"id":id},
            "dataType":"json",
            "success":function(json){
                alert(json.error_msg);
                if(json.error>0){
                    $('#item_'+id).remove();
                }
            }
        });
    }
    function updateweixinmenu(){
        if(!confirm('确认要同步到微信菜单吗？'))return false;
        $.ajax({
            "url":"/myadmin/weixinmenu.do.php?do=updateweixinmenu",
            "type":"get",
            "dataType":"json",
            "success":function(json){
                alert(json.error_msg);
                // if(json.error>0){
                //     $('#item_'+id).remove();
                // }
            }
        });
    }
</script>
</body>
</html>